
import CollisionWorld from "./collision-world";

/**
 * 碰撞管理
 * @param values {autoUpdate,worldCount,config} autoUpdate:boolean,worldCount:number,config:[collisionWorldConfig]
 */
export default class CollisionManager{
    constructor(values={}){
        this._values=values;
        this._init();
    }

    /**
     * 更新碰撞
     * @param deltaTime
     */
    set update(deltaTime){
        if(!this._props.autoUpdate)return;
        let curT=performance.now();
        if(curT-this._timer<deltaTime)return;
        this._timer=curT;
        for(let world of this._worlds){
            if(world)world.update=deltaTime;
        }
    }

    /**
     * 获取碰撞世界列表
     * @returns {[]|Array}
     */
    get worlds(){
        return this._worlds;
    }

    _init(){
        this._initProps();
        this._initVariable();
        this._initComponents();
        this._initialize();
    }
    _initProps(){
        this._props={
            autoUpdate:this._values.autoUpdate!==undefined?this._values.autoUpdate:true,
            worldCount:this._values.worldCount||1,
            config:this._values.config||[null],
        };
    }
    _initVariable(){
        this._worlds=[];
        this._timer=1;
    }
    _initialize(){}
    _initComponents(){
        this._initWorlds();
    }
    _initWorlds(){
        for(let i=0;i<this._props.worldCount;i++){
            let config=this._props.config[i];
            let world=new CollisionWorld(config);
            this._worlds.push(world);
        }
    }
}